﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SDFL.Helper
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Reflection;

    public class ObjectConvertor
    {
        public static T[] ToArray<T>(DataTable dt) where T : class
        {
            List<T> list = new List<T>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(ToObject<T>(row));
            }
            return list.ToArray();
        }

        public static T ToObject<T>(DataRow dr) where T : class
        {
            Type type = typeof(T);
            object obj2 = type.Assembly.CreateInstance(type.FullName);
            foreach (PropertyInfo info in type.GetProperties())
            {
                string name = info.Name;
                if (dr.Table.Columns[name] != null)
                {
                    object obj3 = dr[name];
                    if ((obj3 != null) && !(obj3 is DBNull))
                    {
                        TypeConverter converter = TypeDescriptor.GetConverter(info.PropertyType);
                        if (converter.CanConvertFrom(typeof(string)))
                        {
                            try
                            {
                                obj3 = converter.ConvertFrom(obj3);
                            }
                            catch
                            {
                            }
                        }
                        info.SetValue(obj2, obj3, null);
                    }
                }
            }
            return (T)obj2;
        }

        public static T ToObject<T>(string str)
        {
            if (str == null)
            {
                return default(T);
            }
            Type type = typeof(T);
            TypeConverter converter = TypeDescriptor.GetConverter(type);
            try
            {
                return (T)converter.ConvertFrom(str);
            }
            catch
            {
                return default(T);
            }
        }

        public static T ToObject<T>(string str, T defaultT)
        {
            if (str == null)
            {
                return defaultT;
            }
            Type type = typeof(T);
            TypeConverter converter = TypeDescriptor.GetConverter(type);
            try
            {
                return (T)converter.ConvertFrom(str);
            }
            catch
            {
                return defaultT;
            }
        }

        public static object ToObject(Type type, string str, object defaultT)
        {
            TypeConverter converter = TypeDescriptor.GetConverter(type);
            try
            {
                return converter.ConvertFrom(str);
            }
            catch
            {
                return defaultT;
            }
        }
    }
}
